'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

import unittest, os
from carcomp.framework import copier
from carcomp.directory import localhost


class Test(unittest.TestCase):


    def test_dest_string(self):
        '''Destination string should be formatted correctly'''
        cop = copier.Copier(d_hst='localhost', d_file='/Users/riche/TEST')
        result_string = cop.get_dest_string()
        self.assertEqual('localhost:/Users/riche/TEST', result_string)
    
    def test_src_string(self):
        '''Source string should be formatted correctly'''
        cop = copier.Copier(s_hst='localhost', s_file='/Users/riche/TEST')
        result_string = cop.get_src_string()
        self.assertEqual('localhost:/Users/riche/TEST', result_string)
        
    def test_local_src_string(self):
        '''If no host is given, should assume a local file'''
        cop = copier.Copier(s_file='/Users/riche/TEST')
        result_string = cop.get_src_string()
        self.assertEqual('/Users/riche/TEST', result_string)
        
    def test_local_dest_string(self):
        '''If no host is given, should assume a local file'''
        cop = copier.Copier(d_file='/Users/riche/TEST')
        result_string = cop.get_dest_string()
        self.assertEqual('/Users/riche/TEST', result_string)
        
    def test_local_copy(self):
        '''If no host names should still work as a local copy'''
        s_file='{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root)
        d_file='{}ft/tests/framework/CPTESTOUT.txt'.format(localhost.local_directory.package_root)
        cop = copier.Copier(d_file = d_file, s_file = s_file)
        cop.copy()
        ret = os.access(d_file, os.R_OK)
        if not ret:
            raise Exception('Destination File Not Found')
        os.remove(d_file)
        
    def test_remote_copy_to(self):
        '''Should be able to copy a file to a remote host'''
        s_file='{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root)
        d_file='{}ft/tests/framework/CPTESTOUT.txt'.format(localhost.local_directory.package_root)
        host = 'localhost'
        cop = copier.Copier(s_file = s_file, d_hst = host, d_file = d_file)
        cop.copy()
        ret = os.access(d_file, os.R_OK)
        if not ret:
            raise Exception('Destination File Not Found')
        os.remove(d_file)
        
    def test_remote_copy_from(self):
        '''Should be able to copy a file from a remote host'''
        s_file='{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root)
        d_file='{}ft/tests/framework/CPTESTOUT.txt'.format(localhost.local_directory.package_root)
        host = 'localhost'
        cop = copier.Copier(s_hst = host, s_file = s_file, d_file = d_file)
        cop.copy()
        ret = os.access(d_file, os.R_OK)
        if not ret:
            raise Exception('Destination File Not Found')
        os.remove(d_file)
        
    def test_double_remote_copy(self):
        '''Should be able to copy between two remote locations'''
        s_file='{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root)
        d_file='{}ft/tests/framework/CPTESTOUT.txt'.format(localhost.local_directory.package_root)
        host = 'localhost'
        cop = copier.Copier(s_hst = host, s_file = s_file, d_hst = host, d_file = d_file)
        cop.copy()
        ret = os.access(d_file, os.R_OK)
        if not ret:
            raise Exception('Destination File Not Found')
        os.remove(d_file)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_dest_string']
    unittest.main()